package com.example.demobutterknife.slice;

import butterknife.*;
import com.example.demobutterknife.ResourceTable;
import com.example.demobutterknife.data.ListHolder;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.Color;
import ohos.agp.components.*;
import ohos.agp.text.RichText;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice implements ListContainer.ItemClickedListener {
    private ListHolder listHolder;

    private static int MY_MODULE=0x00201;
    static final HiLogLabel label_tag = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, "Tag");

    @BindView(ResourceTable.Id_btn_test)
    public Button btnTest;

    @BindView(ResourceTable.Id_text_test)
    public Text textTest;

    @BindString(ResourceTable.String_check_string_1)
    public String testString1;

    //测试值int = 100
    @BindInt(ResourceTable.Integer_integer_1)
    public int testInteter1;

    @BindFloat(ResourceTable.Float_float_1)
    public float testFloat1;

    @BindColor(ResourceTable.Color_red)
    public int testColor1;

    @BindArray(ResourceTable.Strarray_size)
    public String[] testStrArray;

    @BindArray(ResourceTable.Intarray_intarray_1)
    public int[] testIntArray;

    @Override
    public void onStart(Intent intent) {
        listHolder = new ListHolder(this);
        ComponentContainer container = listHolder.createComponent();
        setUIContent(container);

        HiLog.info(label_tag, "start bind");
        ButterKnife.bind(this, container);

        btnTest.setText("Check Click");
        testValue();
    }

    private void testValue(){
        HiLog.info(label_tag, "test_string_value:"+testString1);

        String test_string = "string_value:"+testString1;
        String test_integer = "integer_1:"+testInteter1;
        String test_float = "float_1:"+testFloat1;
        String test_color = "color_1"+testColor1;

        String str_array_text = "";
        for (String str : testStrArray){
            if(str_array_text.length()>0){
                str_array_text+=",";
            }
            str_array_text+=str;
        }

        String int_array_text = "";
        for (int value : testIntArray){
            if(int_array_text.length()>0){
                int_array_text+=",";
            }
            int_array_text+=value;
        }

        String test_str_array = "str_array, size:"+ testStrArray.length+" value:"+str_array_text;
        String test_int_array = "int_array, size:"+testIntArray.length+" value:"+int_array_text;

        String text = test_string+"\r\n"+ test_integer+"\r\n"+test_float+"\r\n"+
                test_color+"\r\n"+test_str_array+"\r\n"+test_int_array;

        textTest.setText(text);
        textTest.setMultipleLine(true);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @OnClick(ResourceTable.Id_btn_test)
    public void OnClick(Component component){
        HiLog.info(label_tag, "test_x2:"+component.getId());
        switch (component.getId())
        {
            case ResourceTable.Id_btn_test: {
                ToastDialog dialog = new ToastDialog(getContext());
                dialog.setTitleText("Test Click:"+component.getName());
                dialog.show();
                break;
            }
        }
    }

    @OnItemClick(ResourceTable.Id_list)
    @Override
    public void onItemClicked(ListContainer var1, Component var2, int var3, long var4){
        ToastDialog dialog = new ToastDialog(getContext());
        dialog.setTitleText("Test Click, pos:"+var3);
        dialog.show();
    }
}
